package com.lechi.web.controller;

import com.lechi.web.common.annotation.Log;
import com.lechi.web.common.core.controller.BaseController;
import com.lechi.web.common.core.domain.AjaxResult;
import com.lechi.web.common.core.page.TableDataInfo;
import com.lechi.web.common.enums.BusinessType;
import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.common.utils.SecurityUtils;
import com.lechi.web.service.IIotDeviceSensorRelService;
import com.lechi.web.service.IIotSensorGroupService;
import com.lechi.web.system.domain.IotDeviceSensorBo;
import com.lechi.web.system.domain.IotDeviceSensorRelBo;
import com.lechi.web.system.domain.IotSensorGroupBo;
import com.lechi.web.vo.IotSensorGroupVo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 传感器分组Controller
 *
 * @author yangguang
 */
@RestController
@Validated
@RequestMapping("/iot/sensor/group")
@RequiredArgsConstructor
public class IotSensorGroupController extends BaseController {

    private final IIotSensorGroupService iotSensorGroupService;

    private final IIotDeviceSensorRelService iotDeviceSensorRelService;

    /**
     * 查询传感器分组列表
     *
     * @param iotSensorGroupVo the iot sensor group vo
     * @return the table data info
     */
    @GetMapping("/list")
    public TableDataInfo list(IotSensorGroupVo iotSensorGroupVo) {
        startPage();
        IotSensorGroupBo iotSensorGroupBo = new IotSensorGroupBo();
        BeanUtils.copyProperties(iotSensorGroupVo, iotSensorGroupBo);
        List<IotSensorGroupBo> groupList = iotSensorGroupService.selectIotSensorGroupList(iotSensorGroupBo);
        return getDataTable(groupList);
    }

    /**
     * 获取传感器分组详细信息
     *
     * @param id the id
     * @return the info
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(iotSensorGroupService.selectIotSensorGroupById(id));
    }

    /**
     * 新增传感器分组
     *
     * @param iotSensorGroupVo the iot sensor group vo
     * @return the ajax result
     */
    @PostMapping("/add")
    @Log(title = "新增传感器分组", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody @Valid IotSensorGroupVo iotSensorGroupVo) {
        IotSensorGroupBo iotSensorGroupBo = new IotSensorGroupBo();
        BeanUtils.copyProperties(iotSensorGroupVo, iotSensorGroupBo);
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        iotSensorGroupBo.setCreateBy(username);
        iotSensorGroupService.insertIotSensorGroup(iotSensorGroupBo);
        return AjaxResult.success();
    }

    /**
     * 修改传感器分组
     *
     * @param iotSensorGroupVo the iot sensor group vo
     * @return the ajax result
     */
    @PostMapping("/edit")
    @Log(title = "修改传感器分组", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody @Valid IotSensorGroupVo iotSensorGroupVo) {
        IotSensorGroupBo iotSensorGroupBo = new IotSensorGroupBo();
        BeanUtils.copyProperties(iotSensorGroupVo, iotSensorGroupBo);
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        iotSensorGroupBo.setUpdateBy(username);
        iotSensorGroupService.updateIotSensorGroup(iotSensorGroupBo);
        return AjaxResult.success();
    }

    /**
     * 删除设备观测要素
     *
     * @param ids the ids
     * @return the ajax result
     */
    @DeleteMapping("/{ids}")
    @Log(title = "删除设备观测要素", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        Arrays.asList(ids).forEach(id -> {
            IotDeviceSensorRelBo iotDeviceGroupRel = new IotDeviceSensorRelBo();
            iotDeviceGroupRel.setSensorGroupId(id);
            List<IotDeviceSensorBo> ret = iotDeviceSensorRelService.selectIotDeviceSensorRelList(iotDeviceGroupRel);
            if (ret != null && !ret.isEmpty()) {
                throw new ServiceException(ErrorCodeEnum.DELETE_SENSOR_ERROR.getValue(), ErrorCodeEnum.DELETE_SENSOR_ERROR.getKey());
            }
            iotSensorGroupService.deleteIotSensorGroupById(id);
        });
        return AjaxResult.success();
    }
}
